package com.fivefu.core.skyeye.event.services.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fivefu.base.common.utils.date.DateUtils;
import com.fivefu.core.skyeye.event.entity.DbAiEvent;
import com.fivefu.core.skyeye.event.entity.DbAiEventFailed;
import com.fivefu.core.skyeye.event.services.*;
import com.fivefu.core.skyeye.event.vo.EventImageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class RedisStreamServiceImpl implements RedisStreamService {

    @Autowired
    private DbAiEventService dbAiEventService;
    @Autowired
    private DbAiEventImageService dbAiEventImageService;
    @Autowired
    private DbAiEventFailedService dbAiEventFailedService;

    /**
     * 解析案卷信息
     * @param sceneCode 场景标识
     * @param eventObj
     */
    @Override
    public boolean handleMimirEventInfo(String sceneCode,JSONObject eventObj){
        boolean flag = false;
        String type = eventObj.getString("mq_type");
        if (type.equals("mimir_illegal_parking_event_start")){//事件开始
            String code = eventObj.getString("unique_code");//探头编号
            Long eventId = eventObj.getLong("event_id");//识别事件标识
            String time = eventObj.getString("event_start_time");//事件开始时间

            JSONArray imgArray = eventObj.getJSONArray("images");//事件识别图片
            List<EventImageBean> images = analyEventImages(imgArray);

            LocalDateTime startTime = DateUtils.parse(time, "yyyyMMddHHmmss");

            Long orderId = dbAiEventService.editEventStartInfo(eventId, sceneCode, code, startTime);
            if (orderId != null && !images.isEmpty()){
                //添加图片
                dbAiEventImageService.editEventImage(orderId,images);
            }
            flag = true;
        }else if (type.equals("mimir_illegal_parking_event_append")){//事件追加图像
            Long eventId = eventObj.getLong("event_id");//违停案卷标识

            JSONObject imgObj = eventObj.getJSONObject("images");//违停识别图片
            EventImageBean image = analyEventImage(imgObj);

            DbAiEvent event = dbAiEventService.queryEventByMimirEventId(eventId,sceneCode);
            if (event != null){
                //追加图片
                dbAiEventImageService.editEventImage(event.getId(),image);
                flag = true;
            }
        }else if (type.equals("mimir_illegal_parking_event_end")){//事件结束
            Long eventId = eventObj.getLong("event_id");//识别事件标识
            String time = eventObj.getString("end_time");//事件结束时间
            LocalDateTime endTime = DateUtils.parse(time, "yyyyMMddHHmmss");

            dbAiEventService.editEventEndInfo(eventId,sceneCode,endTime);
            flag = true;
        }else if(type.equals("mimir_illegal_parking_event_failed")){
            Long eventId = eventObj.getLong("event_id");//识别事件标识
            String code = eventObj.getString("unique_code");//探头编号
            String content = eventObj.getString("message_content");//错误信息

            DbAiEvent event = dbAiEventService.queryEventByMimirEventId(eventId,sceneCode);
            if (event != null){
                DbAiEventFailed failed = new DbAiEventFailed();
                failed.setEventId(event.getId());
                failed.setSceneId(sceneCode);
                failed.setCameraId(code);
                failed.setFailedTime(LocalDateTime.now());
                failed.setContent(content);

                //新增错误日志
                dbAiEventFailedService.insertEventFailed(failed);
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 解析案卷图片
     * @param imgArray
     * @return
     */
    @Override
    public List<EventImageBean> analyEventImages(JSONArray imgArray){
        List<EventImageBean> images = new ArrayList<>();
        for (int i = 0 ; i < imgArray.size() ; i ++) {
            JSONObject imgObj = imgArray.getJSONObject(i);
            EventImageBean bean = analyEventImage(imgObj);
            images.add(bean);
        }
        return images;
    }

    /**
     * 解析案卷图片
     * @param imgObj
     * @return
     */
    @Override
    public EventImageBean analyEventImage(JSONObject imgObj){
        EventImageBean bean = new EventImageBean();
        bean.setImageSlicesId(imgObj.getLong("image_slices_id"));
        bean.setImagePath(imgObj.getString("image_path"));
        bean.setImageTime(imgObj.getString("image_time"));
        return bean;
    }

}
